package in.muton.edushare.summary.html;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.CourseGradeDAO;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.GradingItemDAO;
import com.edushare.common.dao.ItemGradeDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseGrade;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.GradingItem;
import com.edushare.common.vo.ItemGrade;
import com.edushare.common.vo.Users;

public class CourseSummary extends WebPage {

	public void init(IModel<CourseInstances> model) {
		this.courseInst = model;
		add(CSSPackageResource.getHeaderContribution(CourseSummary.class,
				"PrinterFriendlyCSS.css"));
		cd = new CourseDataHandler(courseInst);
		
		
		CourseInstances c = courseInst.getObject();
		String semName = c.getSemester().getSemesterName() + ", "
				+ c.getInstanceYear();
		String deptUniv = cd.getOwner().getObject().getDeptName() + ", "
				+ cd.getOwnerUniv().getObject().getUniversityName();

		add(new Label("title", c.getCourse().getCourseName()));
		add(new Label("courseName", c.getCourse().getCourseName()));
		add(new Label("semesterName", semName));
		add(new Label("deptUniv", deptUniv));

		CayenneList<Users> tl = cd.getTeachers();
		add(new Label("facCount", "" + tl.getObject().size()));

		add(new ListView<IModel<Users>>("facList", tl.getObject()) {
			private static final long serialVersionUID = -6658966004457349957L;

			@Override
			protected void populateItem(ListItem<IModel<Users>> arg0) {
				arg0.add(new Label("facName", arg0.getModelObject().getObject()
						.getCompleteUserName()
						+ " ( "
						+ arg0.getModelObject().getObject().getEmail()
						+ " )"));
			}
		});

		add(new Label("credits", c.getCourse().getCreditString()));
		add(new Label("lectureH", c.getCourse().getLectureHours().toString()));
		add(new Label("TutorialH", c.getCourse().getTutorialHours().toString()));
		add(new Label("LabH", c.getCourse().getPracticalHours().toString()));

		CayenneList<Users> sl = cd.getStudents();
		add(new Label("students", "" + sl.getObject().size()));
		add(new MultiLineLabel("courseDesc", c.getCourse().getCourseDesc()).setEscapeModelStrings(false));

		GradingItemDAO gdao = new GradingItemDAO(EduShareSession.get()
				.getSessionORMInterface());
		List<GradingItem> l = gdao.getGradingItemsByCourse(courseInst
				.getObject());
		Collections.sort(l);
		final CayenneList<GradingItem> giList = new CayenneList<GradingItem>(l);
		add(new Label("gradingItemCount", "" + giList.getObject().size()));

		add(new ListView<IModel<GradingItem>>("gradingItemLV", giList.getObject()) {
			private static final long serialVersionUID = -1666460658436001805L;

			@Override
			protected void populateItem(ListItem<IModel<GradingItem>> arg0) {
				GradingItem g = arg0.getModelObject().getObject();
				arg0.add(new Label("gradeItemType", g.getItemTypeString()));
				arg0.add(new Label("gradeItemName", g.getGradingItemName()));
				arg0.add(new Label("gradeItemDesc", g.getGradingItemDesc()).setEscapeModelStrings(false));
				arg0.add(new Label("weightage", g.getWeightage().toString()));
				arg0.add(new Label("maxMarks", g.getMaxMarks().toString()));
			}
		});

		WebMarkupContainer wmc = new WebMarkupContainer("gradeTable") {
			private static final long serialVersionUID = 4920798911538405478L;

			@Override
			public boolean isVisible() {
				return giList.getObject().size() > 0;
			}
		};
		add(wmc);

		wmc.add(new ListView<IModel<GradingItem>>("gradeChartItemLV", giList) {
			private static final long serialVersionUID = 432142833356726464L;

			@Override
			protected void populateItem(ListItem<IModel<GradingItem>> arg0) {
				arg0.add(new Label("itemName", arg0.getModelObject()
						.getObject().getGradingItemName()));
			}
		});

		wmc.add(new ListView<IModel<Users>>("studentGradesLV", sl.getObject()) {
			private static final long serialVersionUID = 3902376312138739564L;

			@Override
			protected void populateItem(final ListItem<IModel<Users>> arg0) {
				final Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("studentName", u.getCompleteUserName()));
				arg0.add(new Label("studentFinalGrade", new Model<String>() {
					private static final long serialVersionUID = -8283557960451403151L;

					@Override
					public String getObject() {
						Users u = arg0.getModelObject().getObject();
						ORMInterface orm = EduShareSession.get()
								.getSessionORMInterface();
						CourseGrade cg = new CourseGradeDAO(orm)
								.getUserCourseGrade(u, courseInst.getObject());
						if (cg == null)
							return "";
						else {
							if (cg.getEvaluationType() == CourseGrade.EVAL_GRADE) {
								return cg.getGradeKey().getGradeAlphabet();
							} else
								return cg.getMarks() + "";
						}
					}
				}));

				arg0.add(new ListView<IModel<GradingItem>>(
						"studentItemGradeLV", giList) {
					private static final long serialVersionUID = -858518889137926217L;

					@Override
					protected void populateItem(
							ListItem<IModel<GradingItem>> arg0) {
						final GradingItem g = arg0.getModelObject().getObject();
						arg0.add(new Label("itemGrade", new Model<String>() {
							private static final long serialVersionUID = -2509367356566608330L;

							@Override
							public String getObject() {
								if (g != null) {
									ORMInterface orm = EduShareSession.get()
											.getSessionORMInterface();
									ItemGrade i = new ItemGradeDAO(orm)
											.getUserGrade(u, g);
									if (i == null)
										return "";
									if (g.getGradeType() == GradingItem.GRADE_LETTER) {
										return i.getGradeKey()
												.getGradeAlphabet();
									} else {
										return "" + i.getMarks();
									}
								}
								return "";
							}
						}));
					}
				});
			}
		});

		add(new GridView<IModel<Users>>("rows", new TableDateProvider<Users>(sl)) {
			private static final long serialVersionUID = 7951517681499851023L;

			@Override
			protected void populateEmptyItem(Item<IModel<Users>> arg0) {
				arg0.add(new Label("studentName", ""));
			}

			@Override
			protected void populateItem(Item<IModel<Users>> arg0) {
				Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("studentName", u.getCompleteUserName()
						+ " ( " + u.getEmail() + " )"));
			}
		});

	}
	
	public CourseSummary(IModel<CourseInstances> cinst){
		init(cinst);
	}

	public CourseSummary(PageParameters params) {
		super(params);
		int id = params.getAsInteger("0", 0);
		CourseInstances cinst = new CourseInstancesDAO
			(EduShareSession.get().getSessionORMInterface()).getCourseInstanceById(id);
		init(new CayenneModel<CourseInstances>(cinst));
	}

	private IModel<CourseInstances> courseInst;
	private CourseDataHandler cd;
}
